package com.example.config;

import com.example.constants.Constants;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

@Component
public class QosListener implements ChannelAwareMessageListener {

    @Override
    @RabbitHandler
    @RabbitListener(queues = Constants.MY_EMAIL_QUEUE_NAME)
    public void onMessage(Message message, Channel channel) throws Exception {

        System.out.println("message.getBody() = " + new String(message.getBody()));

        try {
            System.out.println("处理具体的业务逻辑");

            /**
             * 注释掉 ack,方便查看效果
             */
            //channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), true, true);
        }
    }
}
